// package-market/components/lottery-type-fruit/index.js
import componentBehaviors from '../../../behaviors/component';
import { COMPONENT_OPTIONS, NUMERAL_ZH } from '../../../constants/common';
import { PACKAGE_MARKET_PAGE } from '../../../constants/route';
import { navTo } from '../../../common/env';
import { isEmpty } from '../../../common/utils';
import { dealLotteryAwardData, getOneEmptyValue } from '../../biztools/lottery';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        // 活动id
        activityId: {
            type: String,
            value: '',
        },
        // 通知列表
        noticeList: {
            type: Array,
            value: [],
        },
        // 奖品列表
        awardList: {
            type: Array,
            value: [],
        },
        // 抽奖机会
        count: {
            type: Number,
            value: 0,
        },
        // 抽奖结果
        result: {
            type: Object,
            value: {},
        },
    },
    observers: {
        'result': function (result) {
            if (result && !isEmpty(result)) {
                const { prize } = result;
                const { awardList, showList } = this.data;
                let { index } = getOneEmptyValue();
                if (prize > 0) {
                    index = showList.findIndex(i => i.prize === prize);
                }
                this.animateAction(index);
            }
        },
        'awardList': function(list) {
            if (list && !isEmpty(list)) {
                this.setData({
                    showList: dealLotteryAwardData(list),
                });
            }
        },
    },
    data: {
        NUMERAL_ZH,
        animateValue: 0,
        showList: [],
        showResult: false,
        lotteryResult: {},
    },
    methods: {
        // 开始抽奖
        handleStart(e) {
            this.triggerEvent('start');
        },
        // 旋转动画
        animateAction: function (endIndex) {
            const { showList } = this.data;
            const award = showList.filter(item => item.value == endIndex + 1)[0];
            // 转动顺序为 1=>2=>3=>5=>8=>7=>6=>4 循环
            const animateBaseList = [1, 2, 3, 6, 9, 8, 7, 4];
            let _endIndex = animateBaseList.findIndex(a => award.value == a);
            // 默认从1开始转，转4-6圈 最后给定停的位置
            const cricleNumber = Math.floor(Math.random() * 2) + 4;
            const endArr = animateBaseList.slice(0, _endIndex + 1);
            let targetArray = [];
            for (let i = 0; i < cricleNumber; i++) {
                targetArray = [...targetArray, ...animateBaseList];
            }
            targetArray = [...targetArray, ...endArr];
            // 开始转动动画
            this.animateFun(targetArray, () => {
                this.setData({
                    lotteryResult: award,
                    showResult: true,
                });
            });
        },
        // 动画函数
        animateFun: function (list, callback) {
            const that = this;
            let speed = 50;
            let count = 0;
            !(function interval() {
                setTimeout(() => {
                    count ++;
                    if (count > list.length - 8) {
                        speed = 100;
                    }
                    if (count > list.length - 4) {
                        speed = 200;
                    }
                    if (count == list.length) {
                        callback && callback();
                    } else {
                        interval();
                    }
                    that.setData({
                        animateValue: list[count],
                    })
                }, speed);
            })(this);
        },
        // 关闭
        handleClose(e) {
            if (e.detail) {
                const { activityId } = this.data;
                navTo(PACKAGE_MARKET_PAGE.lotteryRecord, {
                    activityId,
                });
            } else {
                this.setData({
                    showResult: false,
                });
            }
        },
        // 分享
        handleShare() {
            this.triggerEvent('share');
        },
    }
})
